#pragma once

#include "backtest.h"
#include <unordered_map>
#include <deque>

namespace hft {

class QuestDBBacktest : public BacktestSystem {
public:
    QuestDBBacktest(const BacktestConfig& config);
    ~QuestDBBacktest() override = default;

    // 初始化回测系�?
    bool initialize() override;
    
    // 加载历史数据
    bool loadData() override;
    
    // 运行回测
    BacktestResult run(std::shared_ptr<Strategy> strategy) override;

protected:
    // 更新市场数据
    void updateMarketData(const MarketDataMessage& data) override;
    
    // 执行订单
    void executeOrder(const Order& order) override;
    
    // 计算回测指标
    void calculateMetrics() override;
    
    // 记录交易
    void recordTrade(const Trade& trade) override;

private:
    // 内部方法
    bool loadDataFromQuestDB();
    void processMarketData();
    void updatePositions();
    void calculateReturns();
    void calculateDrawdown();
    void calculateSharpeRatio();
    void calculateWinRate();
    void calculateProfitFactor();
    void calculateAverageTrade();
    void calculateAverageHoldingTime();

    // 数据缓存
    std::unordered_map<std::string, std::deque<MarketDataMessage>> data_cache_;
    std::unordered_map<std::string, int> positions_;
    std::unordered_map<std::string, double> last_prices_;
    std::vector<std::pair<std::chrono::system_clock::time_point, double>> equity_curve_;
    std::vector<std::pair<std::chrono::system_clock::time_point, double>> drawdown_curve_;
};

} // namespace hft 
